﻿using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public enum Character
{
    HMBB,
    PDX,
    PLB,
    XLB,
    ZYG
}

public class DialogBubble : MonoBehaviour
{
    private RectTransform _rect;
    private TextMeshProUGUI _text;
    private Image _avatar;

    private int _frameWidth = 50;
    private float _speed = 0.5f;

    void Init()
    {
        _rect = transform.GetComponent<RectTransform>();
        _text = transform.Find("Text").GetComponent<TextMeshProUGUI>();
        _avatar = transform.Find("Avatar").GetComponent<Image>();
    }

    public void Set(string text, Character character, Action cb = null)
    {
        Init();

        Sprite sp = GameManager.Instance.LoadSprite($"{Constants.PathAvatarSprite}/{character}", true);
        _avatar.sprite = sp;
        _text.gameObject.SetActive(false);
        _text.text = text;
        var textRect = _text.GetComponent<RectTransform>();
        _rect.sizeDelta = new Vector2(_rect.sizeDelta.x + _frameWidth / 2, _text.preferredHeight + _frameWidth);
        _text.gameObject.SetActive(true);
        _text.text = string.Empty;
        transform.localScale = Vector3.zero;
        transform.DOScale(Vector3.one, 0.4f).SetEase(Ease.OutBounce).OnComplete(() =>
        {
            _text.text = text;
            cb?.Invoke();
            // _text.DOText(text, text.Length * _speed / 5).SetEase(Ease.Linear).OnComplete(() => { cb?.Invoke(); });
        });
    }
}